<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="全部客户" name="0">
                    <template #label>
                        <el-badge :value="headnumber.total" :max="9999" class="item">
                            <div style="width:65px">全部客户</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="本月跟进" name="1">
                    <template #label>
                        <el-badge :value="headnumber.month" :max="9999" class="item">
                            <div style="width:65px">本月跟进</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="本周跟进" name="2">
                    <template #label>
                        <el-badge :value="headnumber.week" :max="9999" class="item">
                            <div style="width:65px">本周跟进</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'custom.add'"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'custom.delete'"></el-button>
                <el-button type="primary" @click="plExport" v-auth="'custom.import'">批量导入</el-button>
                <el-button type="warning" @click="batch_export" v-auth="'custom.export'">导出</el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-input style="margin:5px 10px" v-model="search.apply_code" placeholder="请输入客户编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.company_name" placeholder="请输入公司名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.contacts" placeholder="请输入对接人" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.mobile" placeholder="请输入手机号码" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.salesman" filterable clearable placeholder="请选择销售">
                        <el-option v-for="item in saletList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border @row-dblclick="table_show" >
                <!-- @setExtend="setExtend" -->
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="ID" prop="id" align="center" width="50"></el-table-column>
                <el-table-column label="客户编号" prop="apply_code" align="center"></el-table-column>
                <el-table-column label="公司名称" prop="company_name" align="center"></el-table-column>
                <el-table-column label="对接人" prop="contacts" align="center"></el-table-column>
                <el-table-column label="手机号码" prop="mobile" align="center"></el-table-column>
                <el-table-column label="所属酒店" prop="hotel.hotel_name" align="center"></el-table-column>
                <el-table-column label="销售" prop="admin.name" align="center"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                <el-table-column label="最新跟进记录" prop="update_time" align="center"></el-table-column>
                <el-table-column label="最后沟通时间" prop="lasttime" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'custom.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'custom.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <!-- <el-button text type="warning" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'custom.detail'">客户详情</el-button> -->
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'custom.share'">共享</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
	<details-dialog v-if="dialog.details" ref="detailsDialog" @success="handleSuccess" @closed="dialog.details = false" :query="{customer_id:this.customer_id,month:1}"></details-dialog>
    <distri v-if="dialog.distri" ref="distriDialog" @success="handleSuccess" @closed="dialog.distri = false"></distri>
    <expoet-all v-if="dialog.expoet" ref="expoetDialog" @success="handleSuccess" @closed="dialog.expoet = false"></expoet-all>
</template>

<script>
import saveDialog from './save.vue'
import detailsDialog from './details.vue'
import Distri from './distri.vue'
import ExpoetAll from '@/views/expoetAll.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,
		detailsDialog,
        Distri,
        ExpoetAll,
	},
	data() {
		return {
			dialog: {
				save: false,
				details: false,
				type: false,
				expoet: false,
			},
			apiObj: this.$API.client.client.list,
			selection: [],
			search: {
				apply_code: null,
				contacts: null,
				salesman: null,
				company_name: null,
				mobile: null,
                follow:null,
			},
            groupId:"0",
            saletList:[],
            customer_id:"",
            headnumber:{},
		}
	},
	mounted() {
        this.getUser()
        this.getNum()
	},
	methods: {
        setExtend(data){
            this.headnumber = data
        },
        // success(res){
		// 	if(res.code==1){
		// 		this.$refs.table.refresh()
		// 		this.$refs.importBox.close()
		// 		this.$message.success("导入成功")
		// 	} else{
		// 		this.$alert(res.message, "提示", {type: 'error'})
		// 	}
		// },
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
            this.customer_id = row.id
			this.dialog.details = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('show').setData(row)
			})
		},
        plExport(){
            this.dialog.expoet = true
            this.$nextTick(() => {
                this.$refs.expoetDialog.open('custom').setData('https://2171.mzth.cn/uploads/files/20240810/20240810112524ae1c81864.xls')
            })
        },// 导出
        async batch_export(){
            var id_all = [];
			this.selection.forEach((item) => {
				id_all.push(item.id)
			})
            let query = {
                id_all:id_all,
                export:2
            }
            var res = await this.$API.client.client.list.get(query)
            if (res.code == 2) {
                const fileUrl = res.data.url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
        // 导出
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.client.client.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.client.client.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'custom') {
				this.$refs.table.refresh()
			}
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
            if(e==1){
                this.search.follow = 'month'
            }else if(e==2){
                this.search.follow = 'week'
            }else{
                this.search.follow = null
            }
            this.$refs.table.upData(this.search)
        },
        // 销售
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.saletList = res.data;
        },
        async getNum(){
            var res = await this.$API.client.client.list.get();
            this.headnumber = res.data.extend
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
